/**
 * Created by hui on 2017/2/26.
 */
//一般直接写在一个js文件中
layui.use(['layer', 'form', 'element'], function () {

    //初始化
    var layer = layui.layer;
    var form = layui.form();
    var element = layui.element;

    //事件监控条件
    var active = {
        'add': function () {
            $.get('add', {}, function (data) {
                layer.open({
                    title: '新增管理员',
                    id: 'new-admin',
                    type: 1,
                    content: data,
                    cancel: function () {

                    }
                });
                form.render()
            });
        },
        'edit': function () {
            $.get('edit', {role_id: $(this).data('role-id')}, function (data) {
                var index = layer.open({
                    title: '权限设置',
                    id: 'auth',
                    type: 1,
                    content: data,
                    maxmin: true,
                    cancel: function () {

                    }
                });
                form.render();
                layer.full(index);
            })
        },
        'module-edit': function () {
            $.get('edit', {module_id: $(this).data('module-id')}, function (data) {
                layer.open({
                    title: '菜单编辑',
                    id: 'module-edit',
                    type: 1,
                    area: ['400px', '600px'],
                    content: data,
                    cancel: function () {

                    }
                });
                form.render();
            })
        }
    };

    //事件监控
    $('.layui-btn').on('click', function () {
        var othis = $(this), method = othis.data('method');
        active[method] ? active[method].call(this, othis) : '';
    });

    $(document).ajaxStart(function () {
        layer.load(2);
    });

    $(document).ajaxStop(function () {
        layer.closeAll('loading');
    });

    $(document).ajaxError(function () {
        layer.msg('服务器出错，请重试');
    });

    //表单提交监控
    form.on('submit(adminAdd)', function (data) {
        $.post('add',data.field,function (data) {
            if (0 === data.code) {
                layer.msg(data.msg);
                return false;
            } else if (1 === data.code) {
                layer.closeAll();
                layer.msg(data.msg);
            }
        });
        return false;
    });

    form.on('submit(roleModule)', function (data) {
        $.ajax({
            type: 'POST',
            url: 'save',
            data: data.field,
            beforeSend: function () {
                layer.msg('操作中', {
                    icon: 16
                    , shade: 0.01
                });
            },
            success: function (data) {
                if (0 === data.code) {
                    layer.msg(data.msg);
                    return false;
                } else if (1 === data.code) {
                    layer.close(adminAdd);
                    layer.msg(data.msg);
                }
            },
            error: function () {
                layer.msg('服务器出错，请重试');
            }
        });
        return false;
    });

    form.on('submit(adminLogin)', function (data) {
        $.ajax({
            type: 'POST',
            url: "login",
            data: data.field,
            beforeSend: function () {
                layer.msg('登录中', {
                    icon: 16
                    , shade: 0.01
                });
            },
            success: function (data, textStatus, jqXHR) {
                if (0 === data.code) {
                    $("input[type='hidden']").val(jqXHR.getResponseHeader('__token__'));
                    layer.msg('登录失败，' + data.msg);
                    if ('用户名或密码错误' == data.msg) {
                        $('.admin-captcha').click();
                    }
                } else if (1 === data.code) {
                    layer.msg('登录成功，' + data.msg);
                    setTimeout(function () {
                        location.href = data.url
                    }, 1500)
                }
            },
            error: function () {
                layer.msg('服务器出错，请重试');
            }
        });
        return false;
    });

    form.on('submit(moduleEdit)', function (data) {
        $.post('edit',data.field,function(data){
            if (0 === data.code) {
                layer.msg(data.msg);
                return false;
            } else if (1 === data.code) {
                layer.msg(data.msg);
            }
        });
    });

    //点击刷新验证码
    $('.admin-captcha').click(function () {
        this.innerHTML = '<img src="/captcha.html" alt="captcha">'
    });
});